package com.guit.hrms.controller;

import com.guit.hrms.po.resume.EmployeeResume;
import com.guit.hrms.po.resume.EmployeeResumeFile;
import com.guit.hrms.po.utilsPO.Result;
import com.guit.hrms.service.EmpResumeService;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/api/user/employeeHistory")
public class EmpResumeController {
    @Autowired
    private EmpResumeService empResumeService;

//    /**
//     * 根据员工工号获取员工履历
//     * @param employeeId 员工工号
//     * @return 员工履历
//     */
//    @GetMapping("/{employeeId}")
//    public ResponseEntity<ByteArrayResource> getEmpResume(@PathVariable @NotNull(message = "employeeId不能为空") String employeeId){
//        try {
//            EmployeeResumeFile employeeResumeFile = empResumeService.findEmpResumeById(employeeId);
//            byte[] resumeBytes = employeeResumeFile.getFileContent();
//            ByteArrayResource resource = new ByteArrayResource(resumeBytes);
//            System.out.println(resource);
//            return ResponseEntity.ok()
//                    .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename="+employeeResumeFile.getFileName()+".pdf")
//                    .contentType(MediaType.APPLICATION_PDF)
//                    .contentLength(resumeBytes.length)
//                    .body(resource);
//        } catch (Exception e) {
//            return ResponseEntity.status(500).body(null);
//        }
//    }

    /**
     * 根据员工工号更新员工履历
     * @param employeeResume 员工履历
     * @return 更新结果
     */
//    @PatchMapping
//    @Validated
//    public Result updateEmpResume(@Valid/*(UpdateMsg.class)*/ @RequestBody EmployeeResume employeeResume){
////        if (employeeResume.getEmployeeId() == null){
////            return Result.error("请输入员工工号");
////        }
//        if (employeeResume.getBasicInfo() == null && employeeResume.getSelfEvaluation() == null){
//            return Result.error("请输入要修改信息");
//        } else {
//           Integer result = empResumeService.updateEmpResume(employeeResume);
//           return result == 1 ? Result.success() : Result.error();
//        }
//    }

    /**
     * 在员工履历中添加记录
     * @param employeeResume 员工履历记录信息
     * @return 添加结果
     */
    @PatchMapping("/add")
    @Validated
    public Result addEmpHistory(@Valid/*(AddMsg.class)*/ @RequestBody EmployeeResume employeeResume){
        if (employeeResume.getEmployeeId() == null){
            return Result.error("请输入员工工号");
        }
        if (employeeResume.getWorkExperiences() == null || employeeResume.getWorkExperiences().isEmpty()) {
            return Result.error("请输入要增加的信息");
        } else {
            Integer result = empResumeService.addEmpResumeMsg(employeeResume);
            return result == 1 ? Result.success() : Result.error();
        }
    }

    /**
     * 导出(下载)员工履历
     * @param employeeId 员工工号
     * @return 员工履历
     */
    @GetMapping("/{employeeId}")
    public ResponseEntity<byte[]> downloadEmpResume(@PathVariable @NotNull(message = "employeeId不能为空") String employeeId){
        EmployeeResumeFile file = empResumeService.downloadEmpResume(employeeId);
        if (file != null) {
            byte[] fileContent = file.getFileContent();
            String encodedFilename = URLEncoder.encode(file.getFileName(), StandardCharsets.UTF_8)
                    .replaceAll("\\+", "%20");
            return ResponseEntity.ok()
                    .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + encodedFilename + ".xlsx\"")
                    .contentType(MediaType.APPLICATION_OCTET_STREAM)
                    .body(fileContent);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 导入员工履历
     * @param employeeResume 员工履历信息
     * @return 导入结果
     */
    @PostMapping
    @Validated
    public Result importEmpResume(@Valid/*({AddMsg.class, UpdateMsg.class})*/ @RequestBody EmployeeResume employeeResume){
        if (employeeResume.getEmployeeId() == null){
            return Result.error("请输入员工工号");
        }
        if (employeeResume.getWorkExperiences() == null || employeeResume.getWorkExperiences().isEmpty()) {
            return Result.error("请输入要导入的信息");
        } else {
            Integer result = empResumeService.importEmpResume(employeeResume);
            return result == 1 ? Result.success() : Result.error();
        }
    }
}
